package xw.qxypt.modules.baseinfo.service.impl;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import xw.qxypt.common.utils.PageUtils;
import xw.qxypt.common.utils.Query;

import xw.qxypt.modules.baseinfo.dao.LhpRoleWaysectRelationDao;
import xw.qxypt.modules.baseinfo.entity.LhpRoleWaysectRelationEntity;
import xw.qxypt.modules.baseinfo.service.LhpRoleWaysectRelationService;


@Service("lhpRoleWaysectRelationService")
public class LhpRoleWaysectRelationServiceImpl extends ServiceImpl<LhpRoleWaysectRelationDao, LhpRoleWaysectRelationEntity> implements LhpRoleWaysectRelationService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<LhpRoleWaysectRelationEntity> page = this.page(
                new Query<LhpRoleWaysectRelationEntity>().getPage(params),
                new QueryWrapper<LhpRoleWaysectRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<LhpRoleWaysectRelationEntity> getByRoleId(String roleId) {
        List<LhpRoleWaysectRelationEntity> list=this.list(
                new QueryWrapper<LhpRoleWaysectRelationEntity>()
                        .eq(StringUtils.isNotBlank(roleId),"ROLE_ID", roleId)
        );
        return list;
    }

    @Override
    public boolean updateRelation(Map<String, Object> params) {
        String roleId= (String) params.get("roleId");
        String waysectIds= (String) params.get("waysectIds");
        String[] waysectId=waysectIds.split(",");
        //删除原有关联
        this.remove(
                new QueryWrapper<LhpRoleWaysectRelationEntity>()
                        .eq(StringUtils.isNotBlank(roleId),"ROLE_ID", roleId)
        );
        List<LhpRoleWaysectRelationEntity> list=new ArrayList<>();
        for (int i = 0; i <waysectId.length ; i++) {
            LhpRoleWaysectRelationEntity entity=new LhpRoleWaysectRelationEntity();
            entity.setRoleId(roleId);
            entity.setWaysectId(waysectId[i]);
            list.add(entity);
        }
        //新增关联
        return this.saveBatch(list);
    }

}
